
package com.baijia.jigou.session;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baijia.jigou.po.OptObj;
import com.baijia.jigou.po.OrgAccount;
import com.baijia.jigou.po.OrgCascadeAccount;
import com.baijia.jigou.util.OrgMemcachedUtil;

import net.rubyeye.xmemcached.exception.MemcachedException;

public class OrgWebSession implements Serializable {

    private static final long serialVersionUID = -2683121849488540792L;
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    private static final String LOGIN_USER_KEY = "LOGIN_USER_KEY";
    private static final String CASCADE_LOGIN_USER_KEY = "CASCADE_LOGIN_USER_KEY";
    private static final String OPT_OBJ_KEY = "OPT_OBJ_KEY";
    private static final int SESSION_EXP_TIME = 24 * 60 * 60;

    private final Map<String, Object> map = new HashMap<String, Object>();

    private String sessionKey;

    private boolean isDestroy = false;

    OrgWebSession(String sessionKey) {
        this.sessionKey = sessionKey;
    }

    public Object getAttribute(String key) {
        return map.get(key);
    }

    public void setAttribute(String key, Object value) {
        map.put(key, value);
        this.flush();
    }

    public boolean isDestroy() {
        return isDestroy;
    }

    public void setDestroy(boolean isDestroy) {
        this.isDestroy = isDestroy;
        try {
            OrgMemcachedUtil.delete(sessionKey);
        } catch (TimeoutException | InterruptedException | MemcachedException e) {
            e.printStackTrace();
        }
    }

    public void clear() {
        map.clear();
    }

    public void setLoginAccount(OrgAccount org) {
        setAttribute(LOGIN_USER_KEY, org);
    }

    public OrgAccount getLoginAccount() {
        return (OrgAccount) getAttribute(LOGIN_USER_KEY);
    }
    
    public void setCascadeLoginAccount(OrgCascadeAccount org) {
        setAttribute(CASCADE_LOGIN_USER_KEY, org);
    }
    
    public OrgCascadeAccount getCascadeLoginAccount() {
        return (OrgCascadeAccount) getAttribute(CASCADE_LOGIN_USER_KEY);
    }

    public void setOptObj(OptObj obj) {
        setAttribute(OPT_OBJ_KEY, obj);
    }

    public OptObj getOptObj() {
        return (OptObj) getAttribute(OPT_OBJ_KEY);
    }

    public void flush() {
        try {
            if (isDestroy) {
                OrgMemcachedUtil.delete(sessionKey);
            } else {
                OrgMemcachedUtil.set(sessionKey, SESSION_EXP_TIME, this);
                logger.trace("flush session-key:{}, value:{} to memcached", sessionKey, this);
            }
        } catch (TimeoutException | InterruptedException | MemcachedException e) {
            logger.error("flush sessionKey:{} to memecached error:{}", sessionKey, e);
        }
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }
}

    